/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.iti.jets.gpts.bean;

import com.iti.jets.gpts.pojo.Comments;
import com.iti.jets.gpts.pojo.Feedback;
import com.iti.jets.gpts.pojo.User;
import com.iti.jets.gpts.service.UserCommentService;
import com.iti.jets.gpts.service.UserFeedbackService;
import com.iti.jets.gpts.service.UserService;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author khalifaabdelaziz
 */
@ManagedBean
@ViewScoped
public class UsersBean implements Serializable {
    private UserService userService;
    public static int selectedID;
    private UserCommentService commentService;
    private ListDataModel<Comments> commentses;
    
    public static int getSelectedID() {
        return selectedID;
    }

    public static void setSelectedID(int selectedID) {
        UsersBean.selectedID = selectedID;
    }
    private ListDataModel<User> users;
   public User u;
   private UserFeedbackService feedbackService;
    private ListDataModel<Feedback> feedbacks ;

    public User getU() {
        return u;
    }

    public void setU(User u) {
        this.u = u;
    }
    public UsersBean() {
        userService = new UserService();
        List<User> allUser = userService.getAllUser();
        users = new ListDataModel<User>(allUser);
    }
    public void  sendData()
    {
         u = users.getRowData();
        selectedID = u.getId();
        System.out.println("rrrrrrrrrrrrrrrrrrrrrrrrrr"+selectedID);
      /*  try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("FeedbacksAndComments");
             return "FeedbacksAndComments";
        } catch (IOException ex) {
          ex.printStackTrace();
        }*/
      //  return "FeedbacksAndComments";
    }

    public ListDataModel<User> getUsers() {
        return users;
    }

    public void setUsers(ListDataModel<User> users) {
        this.users = users;
    }
    public ListDataModel<Feedback> getFeedbacks() {
        return feedbacks;
    }

    public void setFeedbacks(ListDataModel<Feedback> feedbacks) {
        this.feedbacks = feedbacks;
    }
    
    public void loadFeedback(SelectEvent event)
    { 
        User suser = (User)  event.getObject();
        System.out.println("khalifa khalifa khalifa khalifa khalifa khalifa khalfia ");
       if(suser!=null){
       selectedID = suser.getId(); 
         System.out.println("hmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"+selectedID);
          feedbackService= new UserFeedbackService();
         
            int id = getSelectedID();
            System.out.println("ya idddd"+getSelectedID());
            List feedbackUser = feedbackService.getFeedbackUser(id);
            feedbacks= new ListDataModel<Feedback>(feedbackUser);
            
            commentService = new UserCommentService();
             List commnetUser = commentService.getCommnetUser(id);
            commentses = new ListDataModel<Comments>(commnetUser);
        
       }
    }
    
    public void loadComments(SelectEvent event)
    { 
        User suser = (User)  event.getObject();
        System.out.println("khalifa khalifa khalifa khalifa khalifa khalifa khalfia ");
       if(suser!=null){
       selectedID = suser.getId(); 
         System.out.println("hmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"+selectedID);
           commentService = new UserCommentService();
           int id = getSelectedID();
            System.out.println("ya idddd"+getSelectedID());
             List commnetUser = commentService.getCommnetUser(id);
            commentses = new ListDataModel<Comments>(commnetUser);
            }
    }
    
    public ListDataModel<Comments> getCommentses() {
        return commentses;
    }

    public void setCommentses(ListDataModel<Comments> commentses) {
        this.commentses = commentses;
    }
    
    public void deleteuser()
        {
          User userobject = users.getRowData();
            userService.removeUser(userobject.getId()); 
        List<User> allUser = userService.getAllUser();
        users = new ListDataModel<User>(allUser);
        }
}
